<?php
namespace app\index\validate;

use think\Validate;

class User extends Validate
{
    protected $rule = [
        'username'  =>  'require|max:20|min:3',
        'password'  =>  'require|max:20|min:3',
        'repassword'  =>  'require|max:20|min:3',
        'is_check' => 'require'
    ];

    protected $message=[
        'username.require'=>'用户名必填',
        'password.require'=>'密码必填',
        'repassword.require'=>'密码必填',
       'is_check.require'=>'请阅读并同意《用户注册协议》',
        'username.max'=>'用户名最长20位字符',
        'password.max'=>'密码最长20位字符',
        'repassword.max'=>'密码最长20位字符',
        'username.min'=>'用户名最短3位字符',
        'password.min'=>'密码最短3位字符',
        'repassword.min'=>'秘密最短3位字符',
     ];

    protected $scene=[
      'register'=>['username','password','repassword','is_check'],
    ];
    /**
     * 重写 requireIf 便于本次的特殊认证
     */
    protected function requireIf($value, $rule, $data)
    {
        list($field, $val) = explode(',', $rule);
        if (in_array($val,$this->getDataValue($data, $field))) {
            return !empty($value);
        } else {
            return true;
        }
    }
}